package com.demo.netty.init;

import com.demo.netty.http.HttpServer;
import com.demo.netty.mqtt.MqttJavaSever;
import com.demo.netty.protobuf.ProtoBufServer;
import com.demo.netty.tcp.TcpServer;
import com.demo.netty.util.Config;
import org.apache.log4j.Logger;

/**
 * Created by carl on 16/5/29.
 */
public class initServer {
    private static final Logger logger = Logger.getLogger(initServer.class);


    private static final boolean openHttp = Boolean.valueOf(Config.getProperty("openHttp"));
    private static final boolean open_protobuf = Boolean.valueOf(Config.getProperty("open_protobuf"));

    private static final boolean openTcp = Boolean.valueOf(Config.getProperty("openTcp"));

    private static final int http_serverport = Integer.valueOf(Config.getProperty("http_port"));

    private static final int tcp_serverport = Integer.valueOf(Config.getProperty("tcp_serverport"));
    private static final int protobuf_serverport = Integer.valueOf(Config.getProperty("protobuf_serverport"));

    public static void main(String[] args) {
        try {
            if (openHttp) {
                initHttp();
            }
            if (openTcp) {
                initTcp();
            }
            initMqtt();
            if(open_protobuf){
                initProtobuf();
            }
        } catch (Exception e) {
            logger.error("init error", e);
        }
    }

    private static void initHttp() {
        new Thread(new HttpServer(http_serverport)).start();
        logger.info("http接口服务:" + http_serverport);
    }

    private static void initTcp() {
        new Thread(new TcpServer(tcp_serverport)).start();
        logger.info("tcp接口服务:" + tcp_serverport);
    }
    private static void initMqtt(){
        new Thread(new MqttJavaSever()).start();
        logger.info("mqtt服务开启");
        System.out.println("mqtt服务开启");
    }
    private static void initProtobuf(){
        new Thread(new ProtoBufServer(protobuf_serverport)).start();
        logger.info("protobuf_serverport接口服务:" + protobuf_serverport);
        System.out.println("protobuf_serverport接口服务:" + protobuf_serverport);
    }
}
